<?php
session_start();

/**
 * Script used to fetch a list of the pages in the databse.
 * It may be used to only get the parent pages or only the pages owned by a user.
 */

// Return json encoded data
header ('Content-type: application/json');

require_once '../core/db.php';

if(isset($_SESSION['user'])) {              // If user is logged in, get his private pages as well:
    if(isset($_POST['onlyTopLevel'])) {     // If you only want the top-level pages
        $sql = "SELECT pageId, pageName, parent FROM sitePages WHERE (userId=? OR private='0') AND parent='-1'";
    }
    elseif(isset($_POST['forUser'])) {
        $sql = "SELECT pageId, pageName FROM sitePages WHERE userId=?";
    }
    else {
        $sql = "SELECT pageId, pageName, parent FROM sitePages WHERE userId=? OR private='0'";
    }
    $sth = $db->prepare ($sql);
    $sth->execute(array ($_SESSION['user']));
}

else {
    $sql = "SELECT pageId, pageName, parent FROM sitePages WHERE private='0'";
    $sth = $db->prepare ($sql);
    $sth->execute();
}
die (json_encode  ($sth->fetchAll (PDO::FETCH_ASSOC)));
?>

